package com.user.servlet;

import com.user.entity.BookInfo;
import com.user.entity.Users;
import com.user.service.IUserService;
import com.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserReturnBookServlet", value = "/UserReturnBookServlet")
public class UserReturnBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String adminname = null;
        int bookid = Integer.parseInt(request.getParameter("bookid"));
        IUserService userService = new UserServiceImpl();
        boolean result = userService.userReturnBooks(new Users(username, "0"), new BookInfo(bookid));
        if (result) {
            request.setAttribute("msg", "归还成功！");
            adminname = (String) request.getSession().getAttribute("adminname");
        }
        request.getRequestDispatcher("UserBorrowBookListServlet?adminname=" + adminname + "&&username=" + username).forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
